using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebControls;
using EntityLayer;
using InfraStructureLayer;
using BusinessLayer;

public partial class articles_articleDetail : SFPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public override void LanguageLoad()
    {

        if (this.QueryStringId.ToString() == "")
        {
            if (Request.QueryString["Id"] != null && Request.QueryString["Id"] != "")
            {
                this.QueryStringId = Request.QueryString["Id"];
            }
        }
        if (this.QueryStringId != "")
        {

            this.Master.FindControl("ContentPlaceHolderMain").Visible = true;
            lblTitle.Text = "";
            lblArticleSummaryValue.Text = "";
            lblArticleBodyValue.Text = "";
            lblCategoriesValue.Text = "";
            lblPublishDateValue.Text = "";
            lblReadCountValue.Text = "";

            entArticleContents insEntArticleContents = new entArticleContents();
            insEntArticleContents.ArticleId = Convert.ToInt32(new Crypto().DecryptURL(this.QueryStringId));
            busArticleContents insBusArticleContents = new busArticleContents();
            insBusArticleContents.SelectArticleContentsByArticleIdAndLanguageId(insEntArticleContents);
            if (insBusArticleContents.HasErrors)
            {
                MessageBoxShow(insBusArticleContents);
                this.Master.FindControl("ContentPlaceHolderMain").Visible = false;
                return;
            }
            entArticles insEntArticles = new entArticles();
            insEntArticles.ArticleId = insEntArticleContents.ArticleId;
            busArticles insBusArticles = new busArticles();
            insBusArticles.SelectArticlesByArticleId(insEntArticles);
            if (insBusArticles.HasErrors)
            {
                MessageBoxShow(insBusArticles);
                this.Master.FindControl("ContentPlaceHolderMain").Visible = false;
                return;
            }
            lblTitle.Text = insEntArticleContents.ArticleTitle;
            this.Title = insEntArticleContents.ArticleTitle;
            lblArticleSummaryValue.Text = insEntArticleContents.ArticleSummary;
            lblArticleBodyValue.Text = insEntArticleContents.ArticleBody;
            lblPublishDateValue.Text = insEntArticles.PublishDate.ToShortDateString();
            lblReadCountValue.Text = (insEntArticleContents.ArticleReadCount + 1).ToString();

            entArticleArticleCategories insEntArticleArticleCategories = new entArticleArticleCategories();
            insEntArticleArticleCategories.ArticleId = insEntArticles.ArticleId;
            busArticleArticleCategories insBusArticleArticleCategories = new busArticleArticleCategories();
            DataTable insDataTableCategories = insBusArticleArticleCategories.SelectArticleArticleCategoriesByArticleIdAndLanguageId(insEntArticleArticleCategories);
            if (insBusArticleArticleCategories.HasErrors)
            {
                MessageBoxShow(insBusArticleArticleCategories);
                this.Master.FindControl("ContentPlaceHolderMain").Visible = false;
          
                return;
            }
            string s = "";
            foreach (DataRow dr in insDataTableCategories.Rows)
            {
                s += dr["ArticleCategoryName"].ToString() + ",";
            }
            if (s.EndsWith(","))
            {
                s = s.Substring(0, s.Length - 1);
            }
            lblCategoriesValue.Text = s;

            insBusArticleContents.UpdateArticleContentRead(insEntArticleContents);

            entArticleComments insEntArticleComments = new entArticleComments();
            insEntArticleComments.ArticleContentId = insEntArticleContents.ArticleContentId;
            busArticleComments insBusArticleComments = new busArticleComments();
            DataTable insDataTableComments = insBusArticleComments.SelectArticleCommentsByArticleContentId(insEntArticleComments);
            if (insBusArticleComments.HasErrors)
            {
                MessageBoxShow(insBusArticleComments);
                this.Master.FindControl("ContentPlaceHolderMain").Visible = false;
                return;
            }
            sfrComments.DataSource = insDataTableComments;
            sfrComments.DataBind();

            entArticleContentPublishLocations insEntArticleContentPublishLocations = new entArticleContentPublishLocations();
            insEntArticleContentPublishLocations.ArticleContentId = insEntArticleContents.ArticleContentId;
            busArticleContentPublishLocations insBusArticleContentPublishLocations = new busArticleContentPublishLocations();
            DataTable insDataTableContentPublishLocations = insBusArticleContentPublishLocations.SelectArticleContentPublishLocationsByArticleContentId(insEntArticleContentPublishLocations);
            if (insBusArticleContentPublishLocations.HasErrors)
            {
                MessageBoxShow(insBusArticleContentPublishLocations);
                this.Master.FindControl("ContentPlaceHolderMain").Visible = false;
                return;
            }
            sfrContentPublishLocations.DataSource = insDataTableContentPublishLocations;
            sfrContentPublishLocations.DataBind();

        }
        base.LanguageLoad();
    }
    protected void sfrComments_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "DeleteComment")
        {
            entArticleComments insEntArticleComments = new entArticleComments();
            insEntArticleComments.ArticleCommentId = Convert.ToInt32(e.CommandArgument);
            busArticleComments insBusArticleComments = new busArticleComments();
            insBusArticleComments.DeleteArticleCommentsByArticleCommentId(insEntArticleComments);
            if (insBusArticleComments.HasErrors)
            {
                MessageBoxShow(insBusArticleComments);
                return;
            }

            entArticleContents insEntArticleContents = new entArticleContents();
            insEntArticleContents.ArticleId = Convert.ToInt32(new Crypto().DecryptURL(this.QueryStringId));
            busArticleContents insBusArticleContents = new busArticleContents();
            insBusArticleContents.SelectArticleContentsByArticleIdAndLanguageId(insEntArticleContents);
            if (insBusArticleContents.HasErrors)
            {
                MessageBoxShow(insBusArticleContents);
                return;
            }


            insEntArticleComments.ArticleContentId = insEntArticleContents.ArticleContentId;

            DataTable insDataTableComments = insBusArticleComments.SelectArticleCommentsByArticleContentId(insEntArticleComments);
            if (insBusArticleComments.HasErrors)
            {
                MessageBoxShow(insBusArticleComments);
                return;
            }
            sfrComments.DataSource = insDataTableComments;
            sfrComments.DataBind();

        }
    }

    public override void SaveNew()
    {
        Validate();
        if (this.HasErrors)
        {
            MessageBoxShow(this);
            return;
        }
        if (Session["SecurityPicture"].ToString() != txtSecurityPicture.Text)
        {
            MessageBoxShow(StaticMethods.GetErrorMessageMultilanguageString(45, new object[] { }));
            return;
        }
        if (txtArticleCommentUserEmail.Text.Trim().Length > 0)
        {
            if (!WebControls.Validation.ValidateEmail(txtArticleCommentUserEmail.Text))
            {
                MessageBoxShow(StaticMethods.GetErrorMessageMultilanguageString(18, new object[] { lblArticleCommentUserEmail.Text }));
                return;
            }
        }
        if (txtArticleCommentUserWebSite.Text.Trim().Length > 0)
        {
            if (!WebControls.Validation.ValidateWebSite(txtArticleCommentUserWebSite.Text))
            {
                MessageBoxShow(StaticMethods.GetErrorMessageMultilanguageString(18, new object[] { lblArticleCommentUserWebSite.Text }));
                return;
            }
        }

        entArticleContents insEntArticleContents = new entArticleContents();
        insEntArticleContents.ArticleId = Convert.ToInt32(new Crypto().DecryptURL(this.QueryStringId));
        busArticleContents insBusArticleContents = new busArticleContents();
        insBusArticleContents.SelectArticleContentsByArticleIdAndLanguageId(insEntArticleContents);
        if (insBusArticleContents.HasErrors)
        {
            MessageBoxShow(insBusArticleContents);
            return;
        }


        entArticleComments insEntArticleComments = new entArticleComments();
        insEntArticleComments.ArticleCommentBody = txtArticleCommentBody.Text;
        insEntArticleComments.ArticleCommentDate = DateTime.Now;
        insEntArticleComments.ArticleCommentUserEmail = txtArticleCommentUserEmail.Text;
        insEntArticleComments.ArticleCommentUserIpAddress = Request.UserHostAddress;
        insEntArticleComments.ArticleCommentUserName = txtArticleCommentUserName.Text;
        insEntArticleComments.ArticleCommentUserWebSite = txtArticleCommentUserWebSite.Text;
        insEntArticleComments.ArticleContentId = insEntArticleContents.ArticleContentId;
        busArticleComments insBusArticleComments = new busArticleComments();
        insBusArticleComments.InsertArticleComments(insEntArticleComments);
        if (insBusArticleComments.HasErrors)
        {
            MessageBoxShow(insBusArticleComments);
            return;
        }

        DataTable insDataTableComments = insBusArticleComments.SelectArticleCommentsByArticleContentId(insEntArticleComments);
        if (insBusArticleComments.HasErrors)
        {
            MessageBoxShow(insBusArticleComments);
            return;
        }
        sfrComments.DataSource = insDataTableComments;
        sfrComments.DataBind();
        Session["SecurityPicture"] = null;
        txtArticleCommentUserName.Text = "";
        txtArticleCommentUserWebSite.Text = "";
        txtArticleCommentUserEmail.Text = "";
        txtArticleCommentBody.Text = "";
        txtSecurityPicture.Text = "";
        base.SaveNew();
    }
    protected bool GetVisible()
    {
        return (CurrentContext.UserId != null);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
       Save();
    }
}
